
<%@page contentType="text/html" import="java.util.regex.*"
        pageEncoding="UTF-8"%>

<%@page import="javax.jcr.Repository,
        org.apache.jackrabbit.j2ee.RepositoryAccessServlet,
        org.apache.jackrabbit.util.Text,
        javax.jcr.Session,
        javax.jcr.SimpleCredentials,
        javax.jcr.query.Query,
        javax.jcr.query.RowIterator,
        java.text.NumberFormat,
        javax.jcr.query.Row,
        javax.jcr.Node,
        java.net.URLEncoder,
        java.text.SimpleDateFormat,
        java.text.DateFormat,
        java.util.List,
        java.util.ArrayList,
        java.util.Iterator,
        javax.jcr.Value,
        org.ilookinto.util.NodeUtil,
        org.apache.jackrabbit.core.version.InternalVersionManager,
        org.apache.jackrabbit.core.version.InternalVersionManagerImpl,
        org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager,
        org.apache.jackrabbit.core.RepositoryImpl,
        javax.jcr.RepositoryException"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">


<%
    Repository rep;
    Session jcrSession = null;
    try {
      rep = RepositoryAccessServlet.getRepository(pageContext.getServletContext());
      jcrSession = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));
      jcrSession.getWorkspace().getVersionManager();
      RepositoryImpl repi = (RepositoryImpl) rep;
      Node root = jcrSession.getRootNode();
      Node node = emailNode("test1", root);
      if (node != null) {
        out.println("good");
      } else {
        out.println("bad");
      }
      if (!node.hasNode("zwu")) {
        //Node myNode = node.addNode("zwu");
        //myNode.setProperty("test", "test");
        jcrSession.save();
      }
      java.util.Properties props = new java.util.Properties();

      // props.put("mail.smtp.auth", "true");
      //props.put("mail.smtp.starttls.enable", "true");
      javax.mail.Session asession = javax.mail.Session.getInstance(props);
      javax.mail.internet.MimeMessage mm = new javax.mail.internet.MimeMessage(asession) {

        public java.util.Date getSentDate() {
          return new java.util.Date();
        }
      };
      mm.setFrom(new javax.mail.internet.InternetAddress("zhouwu@att.com"));
      mm.setText("testing");
      // mm.setRecipient(type, address);
      org.ilookinto.jr.JCRStoreBean jb = new org.ilookinto.jr.JCRStoreBean();
      jb.setParentNode(node);
      jb.storeMessage(mm);
      jcrSession.save();
    } catch (Throwable e) {
      e.printStackTrace();
      out.println(e);
    } finally {
      if (jcrSession != null) {
        jcrSession.logout();
      }
    }

%>

<%!
  public static Node emailNode(String emailFolder, Node root) {
    Node node = null;
    try {
      String spe = "special folders";
      if (!root.hasNode(spe)) {
        node = root.addNode(spe, "nt:folder");
      } else {
        node = root.getNode(spe);
      }
      if (node.hasNode("emails")) {
        node = node.getNode("emails");
      } else {
        node = node.addNode("emails", "nt:folder");
      }
      if (node.hasNode(emailFolder)) {
        node = node.getNode(emailFolder);
      } else {
        node = node.addNode(emailFolder, "nt:folder");
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return node;

  }
%>

